package com.sc.nft.dao;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.entity.MallActivityShare;
import com.sc.nft.entity.vo.MapAccumulationDetailsVO;
import com.sc.nft.entity.vo.minimall.WxMiniMallDistributionVO;
import com.sc.nft.enums.IncomeTypeEnum;
import com.sc.nft.enums.MallOrderIncomeTypeEnum;
import lombok.extern.slf4j.Slf4j;
import com.sc.nft.entity.MallOrderIncome;
import com.sc.nft.mapper.MallOrderIncomeMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * (sc_mall_order_income)数据DAO
 *
 * @author Sun
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2023-05-09 14:55:38
 */
@Slf4j
@Repository
public class MallOrderIncomeDao extends ServiceImpl<MallOrderIncomeMapper, MallOrderIncome> {

    public List<MallOrderIncome> getListByOrderId(Long orderId) {
        LambdaQueryWrapper<MallOrderIncome> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MallOrderIncome::getOrderId, orderId);
        return this.list(wrapper);
    }

    public List<MallOrderIncome> getSettlementIncomeList() {
        return baseMapper.getSettlementIncomeList();
    }

    public MallOrderIncome getOneByIncomeUserIdAndIncomeTypeAndOrderId(Long incomeUserId, IncomeTypeEnum incomeTypeEnum, Long orderId) {
        LambdaQueryWrapper<MallOrderIncome> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MallOrderIncome::getIncomeUserId, incomeUserId);
        wrapper.eq(MallOrderIncome::getIncomeType, incomeTypeEnum);
        wrapper.eq(MallOrderIncome::getOrderId, orderId);
        wrapper.orderByDesc(MallOrderIncome::getIncomeUserId);
        wrapper.last("limit 1");
        return this.getOne(wrapper);
    }

    public BigDecimal getSumWaitingIncomeByUserId(Long userId) {
        return baseMapper.getSumWaitingIncomeByUserId(userId);
    }

    public BigDecimal mapAccumulation(Long userId) {
        return baseMapper.mapAccumulation(userId);
    }

    public IPage<MapAccumulationDetailsVO> mapAccumulationDetails(Page page, Long id) {
        return baseMapper.mapAccumulationDetails(page, id);
    }

    /**
     *
     *
     * @param id
     * @param incomeTime
     * @return
     */
    public boolean updateIsEfficient(Long id, Date incomeTime, Boolean isEfficient) {
        UpdateWrapper<MallOrderIncome> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", id);
        wrapper.set("income_time", incomeTime);
        wrapper.set("is_efficient", isEfficient);
        return update(new MallOrderIncome(), wrapper);
    }

    public IPage<WxMiniMallDistributionVO> miniMallDistributionUserPage(String userTel, Page page) {
        return baseMapper.miniMallDistributionUserPage(userTel, page);
    }

    public BigDecimal getToBeSettledWxMiniUserCommission(Long wxMiniUserId) {
        return baseMapper.getToBeSettledWxMiniUserCommission(wxMiniUserId);
    }
}